package com.zsxb.crm.controller;

import com.zsxb.crm.annotation.Log;
import com.zsxb.crm.dto.AddUserDTO;
import com.zsxb.crm.dto.PageDTO;
import com.zsxb.crm.dto.UserPageDTO;
import com.zsxb.crm.pojo.User;
import com.zsxb.crm.result.PageResult;
import com.zsxb.crm.result.Result;
import com.zsxb.crm.result.UserResult;
import com.zsxb.crm.service.TransferService;
import com.zsxb.crm.service.UserService;
import com.zsxb.crm.vo.UserDeptVO;
import com.zsxb.crm.vo.UserListSelectVO;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.ArrayList;

/**
 *
 * 2024/10/27
 */
@RestController
@Slf4j
@RequestMapping("/system/user")
@RequiredArgsConstructor
public class UserCoutroller {
    private final TransferService transferService;
    private final UserService userService;

    /**
     * 分页查询用户
     * @param pageDto
     * @return
     */
    @GetMapping("/list")
    public Result<PageResult> pageUser(UserPageDTO pageDto){
        log.info("分页查询用户：{}",pageDto);
        PageResult pageUser = userService.pageUser(pageDto);
        return PageResult.success(pageUser);
    }

    /**
     * 新增用户
     * @param addUserDTO
     * @return
     */
    @PostMapping
    @Log
    public Result addUser(@RequestBody AddUserDTO addUserDTO){
        log.info("新增用户：{}",addUserDTO);
        transferService.addBusiness(userService.addUser(addUserDTO));
        return Result.success();
    }

    /**
     * 修改用户
     * @param addUserDTO
     * @return
     */
    @PutMapping
    @Log
    public Result updateUser(@RequestBody AddUserDTO addUserDTO){
        log.info("修改用户：{}",addUserDTO);
        userService.updataUser(addUserDTO);
        return Result.success();
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public UserResult getById(@PathVariable("id")Long id){

        UserResult userResult = userService.getById(id);
        log.info("查询回显{}",userResult);
        return userResult;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    @Log
    public Result delete(@PathVariable("ids")String ids){
        log.info("删除用户：{}",ids);
        userService.delByIds(ids);
        return Result.success();
    }

    /**
     * 重置密码
     * @param user
     */
    @PutMapping("/resetPwd")
    @Log
    public Result resetPwd(@RequestBody User user){
        log.info("重置密码：{}",user);
        userService.resetPwd(user);
        return Result.success();
    }

    @GetMapping("/listselect")
    public Result<List<UserListSelectVO>> listselect(Long deptId){
        log.info("根据部门id查询用户：{}",deptId);
        List<UserListSelectVO> userListSelectVOList = userService.listselect(deptId);
        return Result.success(userListSelectVOList);
    }
    /**
     * 查询用户相关部门
     */
//    @GetMapping("/listselect")
//     public Result getUserDept(Long deptId){
//         log.info("查询用户相关部门：{}", deptId);
//        ArrayList<UserDeptVO> arrayList = userService.getUserDept(deptId);
//         return Result.success(arrayList);
//    }
}
